<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码 - 宠物医院管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <div class="col-md-2 p-0">
                <c:choose>
                    <c:when test="${sessionScope.currentUser.userType == 'admin'}">
                        <%@ include file="admin-sidebar.jsp" %>
                    </c:when>
                    <c:otherwise>
                        <%@ include file="user-sidebar.jsp" %>
                    </c:otherwise>
                </c:choose>
            </div>

            <!-- 主内容区域 -->
            <div class="col-md-10">
                <div class="main-content p-4">
                    <!-- 页面标题 -->
                    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                        <h1 class="h2">修改密码</h1>
                        <div class="btn-toolbar mb-2 mb-md-0">
                            <c:choose>
                                <c:when test="${sessionScope.currentUser.userType == 'admin'}">
                                    <a href="${pageContext.request.contextPath}/pages/admin/dashboard.jsp" class="btn btn-outline-secondary">
                                        <i class="fas fa-arrow-left"></i> 返回首页
                                    </a>
                                </c:when>
                                <c:otherwise>
                                    <a href="${pageContext.request.contextPath}/pages/user/dashboard.jsp" class="btn btn-outline-secondary">
                                        <i class="fas fa-arrow-left"></i> 返回首页
                                    </a>
                                </c:otherwise>
                            </c:choose>
                        </div>
                    </div>

                    <!-- 成功提示 -->
                    <c:if test="${not empty param.success}">
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle me-2"></i>${param.success}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    </c:if>

                    <!-- 错误提示 -->
                    <c:if test="${not empty param.error}">
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="fas fa-exclamation-circle me-2"></i>${param.error}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    </c:if>

                    <!-- 修改密码表单 -->
                    <div class="row justify-content-center">
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">
                                        <i class="fas fa-key me-2"></i>修改密码
                                    </h5>
                                </div>
                                <div class="card-body">
                                    <form method="post" action="${pageContext.request.contextPath}/user/changePassword" id="changePasswordForm">
                                        <div class="mb-3">
                                            <label for="currentPassword" class="form-label">当前密码 <span class="text-danger">*</span></label>
                                            <div class="input-group">
                                                <input type="password" class="form-control" id="currentPassword" name="currentPassword" required>
                                                <button class="btn btn-outline-secondary" type="button" onclick="togglePassword('currentPassword')">
                                                    <i class="fas fa-eye" id="currentPasswordIcon"></i>
                                                </button>
                                            </div>
                                        </div>

                                        <div class="mb-3">
                                            <label for="newPassword" class="form-label">新密码 <span class="text-danger">*</span></label>
                                            <div class="input-group">
                                                <input type="password" class="form-control" id="newPassword" name="newPassword" required>
                                                <button class="btn btn-outline-secondary" type="button" onclick="togglePassword('newPassword')">
                                                    <i class="fas fa-eye" id="newPasswordIcon"></i>
                                                </button>
                                            </div>
                                            <div class="form-text">密码长度至少6位，建议包含字母和数字</div>
                                        </div>

                                        <div class="mb-3">
                                            <label for="confirmPassword" class="form-label">确认新密码 <span class="text-danger">*</span></label>
                                            <div class="input-group">
                                                <input type="password" class="form-control" id="confirmPassword" name="confirmPassword" required>
                                                <button class="btn btn-outline-secondary" type="button" onclick="togglePassword('confirmPassword')">
                                                    <i class="fas fa-eye" id="confirmPasswordIcon"></i>
                                                </button>
                                            </div>
                                        </div>

                                        <div class="d-flex justify-content-end">
                                            <button type="reset" class="btn btn-secondary me-2">
                                                <i class="fas fa-undo"></i> 重置
                                            </button>
                                            <button type="submit" class="btn btn-primary">
                                                <i class="fas fa-save"></i> 修改密码
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>

                            <!-- 密码安全提示 -->
                            <div class="card mt-4">
                                <div class="card-header">
                                    <h6 class="mb-0">
                                        <i class="fas fa-shield-alt me-2"></i>密码安全提示
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <ul class="mb-0">
                                        <li>密码长度至少6位字符</li>
                                        <li>建议使用字母、数字和特殊字符的组合</li>
                                        <li>不要使用过于简单的密码，如123456、password等</li>
                                        <li>定期更换密码，提高账户安全性</li>
                                        <li>不要在多个网站使用相同密码</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 切换密码显示/隐藏
        function togglePassword(fieldId) {
            const field = document.getElementById(fieldId);
            const icon = document.getElementById(fieldId + 'Icon');
            
            if (field.type === 'password') {
                field.type = 'text';
                icon.className = 'fas fa-eye-slash';
            } else {
                field.type = 'password';
                icon.className = 'fas fa-eye';
            }
        }

        // 表单验证
        document.getElementById('changePasswordForm').addEventListener('submit', function(e) {
            const currentPassword = document.getElementById('currentPassword').value;
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            // 验证当前密码
            if (!currentPassword) {
                alert('请输入当前密码');
                e.preventDefault();
                return;
            }

            // 验证新密码
            if (!newPassword) {
                alert('请输入新密码');
                e.preventDefault();
                return;
            }

            if (newPassword.length < 6) {
                alert('新密码长度至少6位');
                e.preventDefault();
                return;
            }

            // 验证确认密码
            if (!confirmPassword) {
                alert('请确认新密码');
                e.preventDefault();
                return;
            }

            if (newPassword !== confirmPassword) {
                alert('两次输入的新密码不一致');
                e.preventDefault();
                return;
            }

            // 检查新密码是否与当前密码相同
            if (currentPassword === newPassword) {
                alert('新密码不能与当前密码相同');
                e.preventDefault();
                return;
            }
        });
    </script>
</body>
</html>
